package com.kayt.cyberBar.cuser.business.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.kayt.cyberBar.common.PageData;
import com.kayt.cyberBar.common.dao.EntityDao;
import com.kayt.cyberBar.common.property.PublicCodeConfigInfo;
import com.kayt.cyberBar.cuser.business.dao.IUserDetailDao;
import com.kayt.cyberBar.cuser.business.entity.po.BUserDetail;

@Repository("userDetailDao")
public class UserDetailDaoImpl implements IUserDetailDao {
	@Autowired
	private EntityDao entityDao;

	@Autowired
	private PublicCodeConfigInfo configInfo;

	@Override
	public Integer save(BUserDetail entity) throws SQLException {
		if (entity != null) {
			entityDao.save(entity);
			return 0;
		} else {
			return -1;
		}
	}

	@Override
	public Integer update(BUserDetail entity) throws SQLException {
		if (entity != null) {
			entityDao.update(entity);
			return 0;
		} else {
			return -1;
		}
	}

	@Override
	public Integer delete(Long id) {
		if (id != null) {
			BUserDetail BUserDetail = findById(id);
			if (BUserDetail != null) {
				entityDao.delete(BUserDetail);
				return 0;
			} else {
				return -1;
			}
		} else {
			return -1;
		}
	}

	@Override
	public BUserDetail findById(Long id) {
		if (id != null) {
			return entityDao.findById(id, BUserDetail.class);
		} else {
			return null;
		}
	}

	@Override
	public BUserDetail findByCode(String userCode) throws SQLException {
		if (StringUtils.isNotEmpty(userCode)) {
			List<BUserDetail> bUserList = entityDao.getPagingDataBySql(BUserDetail.class, -1, -1,
					"select * from b_user_detail where USER_CODE = '" + userCode + "'", null);
			if (bUserList != null && bUserList.size() == 1) {
				return bUserList.get(0);
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	@Override
	public BUserDetail findByWorkCode(String workCode) throws SQLException {
		if (StringUtils.isNotEmpty(workCode)) {
			List<BUserDetail> bUserList = entityDao.getPagingDataBySql(BUserDetail.class, -1, -1,
					"select u.* from b_user_detail u , b_sheet_user_work uw where u.USER_CODE = uw.USER_CODE and uw.WORK_CODE = '"
							+ workCode + "'",
					null);
			if (bUserList != null && bUserList.size() == 1) {
				return bUserList.get(0);
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	public PageData<BUserDetail> findUsersByStoreCode(Integer pageNum, Integer pageSize, String storeCode,String departCode,
			List<String> notRoleCodeList) throws SQLException {
		String roleCodeSql = "";
		String departSql = "";
		if(StringUtils.isNotEmpty(departCode)){
			departSql = " and b1.departCode = '"+departCode+"' ";
		}
		if (notRoleCodeList != null && notRoleCodeList.size() != 0) {
			Iterator<String> roleCodeIter = notRoleCodeList.iterator();
			while (roleCodeIter.hasNext()) {
				String code = roleCodeIter.next();
				if (StringUtils.isEmpty(roleCodeSql)) {
					roleCodeSql += " and ur.roleCode not in ('" + code + "'";
				} else {
					roleCodeSql += ",'" + code + "'";
				}
			}
			roleCodeSql += ")";
		}
		String hql = "SELECT userDetail as user FROM BUserDetail userDetail,BUser u,BOrganizationDepartUser b1, BOrganizationStoreDepart b2 "
				+ " , BUserRole ur WHERE userDetail.userCode = u.userCode and u.userCode = b1.userCode AND b1.storeDepartCode = b2.storeDepartCode "
				+ "AND b2.storeCode = '" + storeCode + "' and u.statusCode != '" + configInfo.getUserStatusDelete()
				+ "'" + roleCodeSql + departSql + " and u.userCode = ur.userCode";
		String countSql = "SELECT count(userDetail.id) as count from BUserDetail userDetail,BUser u,BOrganizationDepartUser b1, BOrganizationStoreDepart b2 "
				+ " , BUserRole ur WHERE userDetail.userCode = u.userCode and u.userCode = b1.userCode AND b1.storeDepartCode = b2.storeDepartCode "
				+ "AND b2.storeCode = '" + storeCode + "' and u.statusCode != '" + configInfo.getUserStatusDelete()
				+ "'" + roleCodeSql + departSql + " and u.userCode = ur.userCode";
		List<Map<String, Object>> userList = entityDao.getPagingDataByHql(pageNum, pageSize, hql, null);
		List<Map<String, Object>> countResult = entityDao.getPagingDataByHql(pageNum, pageSize, countSql, null);
		Long count = (Long) countResult.get(0).get("count");
		PageData<BUserDetail> pageData = new PageData<>();
		List<BUserDetail> resultUser = new ArrayList<>();
		Iterator<Map<String, Object>> mapIter = userList.iterator();
		while (mapIter.hasNext()) {
			resultUser.add((BUserDetail) mapIter.next().get("user"));
		}
		pageData.setPageList(resultUser);
		pageData.setTotal(count);
		return pageData;
	}

	@Override
	public Map<String, BUserDetail> findUserMapByStoreCode(Integer pageNum, Integer pageSize, String storeCode,String departCode)
			throws SQLException {
		PageData<BUserDetail> pageDate = findUsersByStoreCode(pageNum, pageSize, storeCode,departCode, null);
		if (pageDate != null && pageDate.getTotal() != 0) {
			Map<String, BUserDetail> userMap = new HashMap<>();
			List<BUserDetail> userList = pageDate.getPageList();
			Iterator<BUserDetail> userIter = userList.iterator();
			while (userIter.hasNext()) {
				BUserDetail user = userIter.next();
				userMap.put(user.getUserCode(), user);
			}
			return userMap;
		} else {
			return new HashMap<String, BUserDetail>();
		}
	}
}
